package com.fqcar.fqdf.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.android.volley.Response;
import com.baidu.location.BDLocation;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMap.OnMapClickListener;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.fqcar.fqdf.R;
import com.fqcar.fqdf.service.RequestOptService;
import com.fqcar.fqdf.util.Constants;
import com.fqcar.fqdf.util.DialogUtil;
import com.fqdf.framework.android.net.response.NetBaseWrapper;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 充电桩首页
 */
public class ModifyCarPosActivity extends MapLocationActivity implements BaiduMap.OnMapStatusChangeListener, OnMapClickListener, OnGetGeoCoderResultListener {

    @BindView(R.id.baidumap)
    MapView baidumapView;
    @BindView(R.id.title_txt)
    TextView titleTxt;

    //定位
    private BDLocation location = null;
    //逆地理
    private GeoCoder geoCoder;

    private int carId;
    private double centerLat;
    private double centerLon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_car_pos);
        ButterKnife.bind(this);
        //初始化数据
        initData();
        // 调用父类的地图初始化
        initMap(baidumapView);
        // 初始化本地地图的个性设置
        initMyMap();
        // 初始化请求数据
        initReqData();
    }

    // 初始化地图设置
    private void initMyMap() {
        mBaiduMap.setOnMapStatusChangeListener(this);
        mBaiduMap.setOnMapClickListener(this);
    }

    private void initData() {
        titleTxt.setText(getString(R.string.title_modify_car_pos));
        carId = getIntent().getIntExtra(Constants.BUNDLE_DATA, 0);
        geoCoder = GeoCoder.newInstance();
        geoCoder.setOnGetGeoCodeResultListener(this);
    }

    /**
     * 初始化请求数据
     */
    private void initReqData() {
        // 开始进行定位
        startLocationUpdate();

        // 根据定位获取网点数据以及更新对应的城市代码。如果定位数据为空，则进行1秒等待后继续调用
        getThreadHandler().post(new Runnable() {
            @Override
            public void run() {
                if (getCurValidAndCityLocation() != null) {
                    location = getCurValidAndCityLocation();
                    MapStatus mMapStatus = new MapStatus.Builder()
                            .target(new LatLng(location.getLatitude(), location.getLongitude()))
                            .zoom(MAP_ZOOM_DEFAULT)
                            .build();
                    //定义MapStatusUpdate对象，以便描述地图状态将要发生的变化
                    MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
                    //改变地图状态
                    mBaiduMap.animateMapStatus(mMapStatusUpdate);

                } else {
                    startLocationUpdate();
                    getThreadHandler().postDelayed(this, 1000);
                }
            }
        });
    }

    @OnClick({R.id.main_back_iv,R.id.main_function_location, R.id.commit_btn})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_back_iv:
                finish();
                break;
            case R.id.main_function_location:
                BDLocation curLocation = getCurValidAndCityLocation();
                // 定位当前位置，如果当前位置有效的话地图移动到我的定位
                if (curLocation != null) {
                    changePosition(curLocation.getLatitude(), curLocation.getLongitude());
                }
                break;
            case R.id.commit_btn:
                updateCarPosition(carId, centerLat, centerLon);
                break;

            default:
                break;
        }
    }

    @Override
    public void onMapStatusChangeStart(MapStatus mapStatus) {

    }

    @Override
    public void onMapStatusChangeStart(MapStatus mapStatus, int i) {

    }

    @Override
    public void onMapStatusChange(MapStatus mapStatus) {

    }

    @Override
    public void onMapStatusChangeFinish(MapStatus mapStatus) {
        centerLat = mapStatus.target.latitude;
        centerLon = mapStatus.target.longitude;
    }


    private void updateCarPosition(int carId, double lat, double lng) {
        RequestOptService.updateCarPosition(this, carId, lat, lng, new Response.Listener<NetBaseWrapper>() {
            @Override
            public void onResponse(NetBaseWrapper response) {
                if (!handleResponse(response)) {
                    DialogUtil.ToastMessage(ModifyCarPosActivity.this, "上报修改成功！");
                    ModifyCarPosActivity.this.finish();
                }

            }
        }, getDefaultErrorListener());

    }


    @Override
    public void onMapClick(LatLng latLng) {
    }

    @Override
    public boolean onMapPoiClick(MapPoi mapPoi) {
        return false;
    }


    @Override
    public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {
        if (geoCodeResult == null || geoCodeResult.error != SearchResult.ERRORNO.NO_ERROR) {
            //没有检索到结果
        }
    }

    @Override
    public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {
        if (reverseGeoCodeResult == null || reverseGeoCodeResult.error != SearchResult.ERRORNO.NO_ERROR) {
            //没有检索到结果
        } else {
//            stubViewHelper.updateCarAddress(reverseGeoCodeResult.getAddress());
        }
    }

}
